let express = require('express')
let router = express.Router();
let async = require('async');
let Manager = require('../modules/mongo');
let DBManager =new Manager();

router.post('/addbook', (req, res, next) => {
    let req_data = req.body
    let _bookType = {
        bookType: req_data.bookType
    }
    let params ={
        bookName: req_data.bookName,
        author: req_data.author,
        publicationgDate: req_data.publicationgDate,
        introduction: req_data.introduction,
        status: req_data.status,
        create_time: new Date(),
        update_time: new Date() 
    }
    async.waterfall([
        function getbookType (cb) {
            DBManager.findOne('booksType', _bookType, result => {
                if(result.success){
                    cb(null, result.result._id+'')
                    return
                }
                cb({success:false, code:2, message:'图书类型不存在'})
            })
        },
        function insertBook (n,cb) {  
            params.bookTypeID = n
            DBManager.insert('booksInfor', params, result => {
                if(result.success){
                    cb(null, {success:true, code: 0, message:'新增图书成功'})
                    return
                }
                cb({success:false, code:1, message:'新增图书失败'})
            })
        }
    ],(err, result) => {
        if(err){
            res.send(err)
            return
        }
        res.send(result)
    })
})

router.get('/', (req, res, next) => {
    let req_data = req.query
    let _bookType = {
        bookType: {$regex:req_data.bookType?req_data.bookType:'', $options: 'i'}
    }
    let params = {
        bookName: {$regex: req_data.bookName?req_data.bookName:'', $options: 'i'},
        author: {$regex: req_data.author?req_data.author:'', $options: 'i'},
        status: {$regex: req_data.status?req_data.status:'', $options: 'i'}
    }
    async.parallel({
        _getbookType: function(cb){
           DBManager.find('booksType', _bookType, {}, result => {
               if(result.success){
                   cb(null, result.result)
                   return
               }
               cb({success:false, code:2, message:'查询书籍类型失败'})
           })
        },
        _getbookInfor: function(cb){
            DBManager.find('booksInfor', {$or: [params]}, {}, result => {
                if(result.success){
                    cb(null, result.result)
                    return 
                }
                cb({success:false, code:1, message:'查询书籍失败'})
            })
        }
    },(err, result)=>{
        let data = []
        if(err){
            res.send(err)
            return 
        }
        result._getbookInfor.map(item =>{
            let hasbookType = result._getbookType.find(currentValue => {
                return currentValue._id+'' === item.bookTypeID
            })
            if(!hasbookType){
                return
            }
            let obj = Object.assign({}, hasbookType, item)
            data.push(obj)
        })  
        res.send({success: true, result: data, message:'查询成功'})
    })
})

router.post('/deletebook', (req, res, next) => {
    let req_data = req.body
    DBManager.delete('booksInfor',{_id: req_data._id})
    .then((response) => {
        if(response.success){
            res.send({success: true, code: 0, message: '删除成功'})
            return
        }
        res.send({success: false, code: 1, message: '删除失败'})
    }).catch(err => {
        res.send(err)
    })
})

router.post('/updatabook', (req, res, next) => {
    let req_data = req.body
    let _bookType = {
        bookType: req_data.bookType
    }
    let params ={
        bookName: req_data.bookName,
        author: req_data.author,
        publicationgDate: req_data.publicationgDate,
        introduction: req_data.introduction,
        status: req_data.status,
        update_time: new Date() 
    }
    let whereData = {_id: req_data._id}
    async.waterfall([
        function getbookType (cb) {
            DBManager.findOne('booksType', _bookType, result => {
                if(result.success){
                    cb(null, result.result._id+'')
                    return
                }
                cb({success:false, code:2, message:'图书类型不存在'})
            })
        },
        function updateBook (n,cb) {  
            params.bookTypeID = n
            DBManager.update('booksInfor', whereData, {$set: params})
            .then(respone => {
                if(respone.success){
                    cb(null,{success:true, code:0, message:'更新成功'})
                    return 
                }
                cb({success:false, code:1, message:'更新失败'})
            }).catch(err => {
                cb({success:false, code:2, message:'更新失败'})
            })
        }
    ],(err, result) => {
        if(err){
            res.send(err)
            return
        }
        res.send(result)
    })
})

router.post('/batchDeletebook', (req, res, next) => {
    let req_data = req.body
    DBManager.batchDelete('booksInfor',req_data)
    .then((response) => {
        if(response.success){
            res.send({success: true, code: 0, message: '删除成功'})
            return
        }
        res.send({success: false, code: 1, message: '删除失败'})
    }).catch(err => {
        res.send(err)
    })
})

module.exports = router
